﻿package com.wandian.view
{
	import com.*;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.controller.ChatRoomCommand;
	import com.wandian.controller.LobbyCommand;
	import com.wandian.controller.LoginCommand;
	import com.wandian.controller.RankCommand;
	import com.wandian.controller.SocketCommand;
	import com.wandian.controller.TipCommand;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.view.mediator.FriendAndRoomPlayerMediator;
	import com.wandian.view.mediator.GameRoomMediator;
	import com.wandian.view.mediator.LobbyMediator;
	import com.wandian.view.mediator.LoginMediator;
	import com.wandian.view.mediator.TopMediator;
	
	import flash.display.*;
	import flash.external.*;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

	/**
	 * 游戏视图入口
	 * @author long
	 * @Create  2012-10-01 下午03:12:00
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class MainMediator extends Mediator
	{
//        private var _gameData:GameDataProxy;
//        private var _global:GlobalData;
		public static const NAME:String="GameMainMediator";

		public function MainMediator(viewComponent:Object=null)
		{
//            this._global = GlobalData.getInstance();
			super(NAME, viewComponent);
//            this._gameData = facade.retrieveProxy(GameDataProxy.NAME) as GameDataProxy;
			this.initialize();
			return;
		}

		override public function onRegister():void
		{
//            try
//            {
//                ExternalInterface.addCallback("showWeiBo", this.showWeiBoHandler);
//            }
//            catch (e:Error)
//            {
//            }
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.LOBBYMEDIATOR_CONNECT_SOCKET_SUCC, CLZNoticeName.REDIRECTTO_LOGIN, CLZNoticeName.MAINMEDIATOR_SOCKET_UNCONNECTED, CLZNoticeName.RELOAD_WINDOW_PAGE, CLZNoticeName.ON_PAY_VIP, CLZNoticeName.GO_VERIFICATION, CLZNoticeName.SHOW_WEIBO_PANEL_TIP];
		}

		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case CLZNoticeName.LOBBYMEDIATOR_CONNECT_SOCKET_SUCC:
				{
					this.sendNotification(CLZNoticeName.SHOW_FULL_TIP, {tip: "连接成功", isShowBtn: false, btnName: "", strNoticeName: ""});
					this.sendNotification(CLZNoticeName.HIDE_FULL_TIP);
					this.sendNotification(CLZNoticeName.SHOW_TOP);

					//设置全局标识，连接成功
					break;
				}
				case CLZNoticeName.MAINMEDIATOR_SOCKET_UNCONNECTED:
				{
//					trace("未连接成功");
					//设置全局标识，连接成功
//					var data:Object={okNoticeName: CLZNoticeName.REDIRECTTO_LOGIN, tip: "网络连接错误，重新登录", tipTitle: "网络提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
//					this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
					break;
				}
				case CLZNoticeName.REDIRECTTO_LOGIN:
				{
//					if (Texas.I._mcTableView.contains(this.mcChatRoom))
//						Texas.I._mcTableView.removeChild(this.mcChatRoom);
//					this.facade.removeMediator(ChatRoomMediator.NAME);
//					this.sendNotification(CLZNoticeName.LOGINCOMMAND_ENTER_LOGIN);
					break;
				}
				case CLZNoticeName.RELOAD_WINDOW_PAGE:
				{
//					if (this.facade.hasMediator(GameRoomMediator.NAME))
//					{
					this.facade.removeMediator(GameRoomMediator.NAME);
//						var gameRoom:GameRoomMediator=this.facade.retrieveMediator(GameRoomMediator.NAME) as GameRoomMediator;
					Texas.I._mcGameView.removeAll();
//					}
//					if (this.facade.hasMediator(LobbyMediator.NAME)){
//						var lobby:LobbyMediator=this.facade.retrieveMediator(LobbyMediator.NAME) as LobbyMediator;
					texas._mcTableView.removeAll();
					this.facade.removeMediator(LobbyMediator.NAME);
//					}
					this.facade.removeMediator(FriendAndRoomPlayerMediator.NAME);
					this.facade.removeMediator(TopMediator.NAME);
					texas._mcTopView.removeAll();
					texas._mcWindosView.removeAll();
//					facade.removeProxy(InRoomPlayerDataProxy.NAME);
//					facade.registerProxy(new InRoomPlayerDataProxy());
//					var _inRoomData:InRoomPlayerDataProxy=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
//					_inRoomData.reset();
					this.facade.removeMediator(LoginMediator.NAME);
					this.sendNotification(CLZNoticeName.LOGINCOMMAND_ENTER_LOGIN, this.texas);
					break;
				}
				default:
				{
					break;
				}
			}
			/* var _loc_2:Sprite = null;
			 var _loc_3:PopUpPanel = null;
			 switch(param1.getName())
			 {
				 case CLZNoticeName.RELOAD_WINDOW_PAGE:
				 {
					 ExternalInterface.call("window.location.reload");
					 break;
				 }
				 case CLZNoticeName.ON_PAY_VIP:
				 {
					 if (this._global.userInfo.userVo.gp != GameConfig.QZONE_SID)
					 {
					 }
					 if (this._global.userInfo.userVo.gp == GameConfig.FRIEND_SID)
					 {
						 this.sendNotification(CLZNoticeName.SHOW_SHOPING_VIP);
					 }
					 else
					 {
						 JSFUN.payFunc();
					 }
					 break;
				 }
				 case CLZNoticeName.GO_VERIFICATION:
				 {
					 JSFUN.goVerificationFunc();
					 break;
				 }
				 case CLZNoticeName.SHOW_WEIBO_PANEL_TIP:
				 {
					 facade.registerCommand(CLZNoticeName.WEIBO_REQ, PrizeCommand);
					 _loc_2 = GlobalData.getSprite("WeiBoPanel");
					 _loc_3 = new PopUpPanel(_loc_2, CLZNoticeName.WEIBO_REQ, CLZNoticeName.HIDE_WEIBO_PANEL_TIP);
					 break;
				 }
				 case CLZNoticeName.HIDE_WINDOWS_TIP:
				 {
					 facade.removeCommand(CLZNoticeName.WEIBO_REQ);
					 break;
				 }
				 default:
				 {
					 break;
				 }
			 }*/
			return;
		}

		private function showWeiBoHandler():void
		{
			this.sendNotification(CLZNoticeName.SHOW_WEIBO_PANEL_TIP);
			return;
		}

		/**
		 * 主视图初始化，采用消息命令方式
		 */
		private function initialize():void
		{
			trace("MainMediator.initialize()");

			//进入大厅
			facade.registerCommand(CLZNoticeName.LOBBYCOMMAND_ENTER_LOBBY, LobbyCommand);
			facade.registerCommand(CLZNoticeName.SOCKETCOMMAND_CONNECT_SOCKET, SocketCommand); //网络通信命令
			facade.registerCommand(CLZNoticeName.SOCKETCOMMAND_REQ_LOGIN, SocketCommand); //请求登陆		
			facade.registerCommand(CLZNoticeName.SOCKETCOMMAND_ROOM_LIST_REQ, SocketCommand); //房间列表
			facade.registerCommand(CLZNoticeName.SOCKETCOMMAND_FRIEND_LIST_REQ, SocketCommand); //请求好友	

			facade.registerCommand(CLZNoticeName.TIPCOMMAND_ADD_TIP, TipCommand); //系统提示框

			//聊天室

			/*facade.registerCommand(CLZNoticeName.SOCKETCOMMAND_REQ_SENDMESSAGE, SocketCommand); //请求登陆
			facade.registerCommand(CLZNoticeName.SOCKETCOMMAND_REQ_TESTAUTOADD, SocketCommand); //请求登陆
			facade.registerCommand(CLZNoticeName.CHATROOMCOMMAND_ENTER_CHATROOM, ChatRoomCommand);*/

			facade.registerCommand(CLZNoticeName.SHOW_RANK, RankCommand);
			facade.registerCommand(CLZNoticeName.REGISTER, SocketCommand); //这个家伙，进游戏之前一定要注册
			facade.registerCommand(CLZNoticeName.REGISTER_MATCH, SocketCommand);
			facade.registerCommand(CLZNoticeName.OUT_GAME_ROOM, SocketCommand);
			facade.registerCommand(CLZNoticeName.SITDOWN, SocketCommand);
			facade.registerCommand(CLZNoticeName.SETROOMINFO, SocketCommand);
			facade.registerCommand(CLZNoticeName.STAND_UP, SocketCommand);
			facade.registerCommand(CLZNoticeName.FLOD, SocketCommand);
			facade.registerCommand(CLZNoticeName.CHECK, SocketCommand);
			facade.registerCommand(CLZNoticeName.RAISE, SocketCommand);
			facade.registerCommand(CLZNoticeName.CALL, SocketCommand);
			facade.registerCommand(CLZNoticeName.ALLIN, SocketCommand);
			facade.registerCommand(CLZNoticeName.CHAT, SocketCommand);
			facade.registerCommand(CLZNoticeName.AGAIN_SIT_BUYIN, SocketCommand);
			facade.registerCommand(CLZNoticeName.GIVE_CHIP_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.GIVE_GIFT_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.USE_PROP_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.RENEW_GIFT_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.GIFT_UPDATE_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.GIFT_CANCEL_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.FRIEND_RETURN_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.FRIEND_TO_REQUEST_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.TRACK_ROOM, SocketCommand);
			facade.registerCommand(CLZNoticeName.FACE_REQ, SocketCommand);
			facade.registerCommand(CLZNoticeName.INIT_SET, SocketCommand);
			facade.registerCommand(CLZNoticeName.FAST_ENTER_GAME_REQ, SocketCommand);
//			
//			this.sendNotification(CLZNoticeName.ROOM_TABLE);		

			this.sendNotification(CLZNoticeName.TIPCOMMAND_ADD_TIP);
			//注册登录ui，登录界面命令
			this.sendNotification(CLZNoticeName.LOGINCOMMAND_ENTER_LOGIN, this.texas);
			//通知连接网络
			if (!GameData.isConnectSocket)
				this.sendNotification(CLZNoticeName.SOCKETCOMMAND_CONNECT_SOCKET);
			return;
		}

		private function get texas():Texas
		{
			return viewComponent as Texas;
		}

	}
}
